<?php
	require_once('connection/connector.php');
	require_once('classes/picture.class.php');
	
	$connect = new DbConnector();
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>
			Gallery
		</title>
		<meta charset="windows-1251">
	</head>
	<body>
		<header>
			<h1>
				PHP based gallery with resizing function
			</h1>
		</header>
		<section>
			<article>
			<?php
				$action = $_GET['action'];
				
				if($action == 2 && isset($_POST['submit']))
				{
					$description = htmlspecialchars($_POST['description']);
					$image = $_FILES['image'];
									
					if(($image["type"] == "image/gif")|| ($image["type"] == "image/jpeg") || ($image["type"] == "image/pjpeg")||($image["type"] == "image/png") || ($image["type"] == "image/bmp") || ($image["type"] == "image/BMP") && ($image["size"] < 4000000))
					{
						switch(end(explode(".", strtolower($image['name']))))
						{
							case 'gif': $format = 'gif';break;
							case 'jpeg': $format = 'jpeg';break;
							case 'jpg': $format = 'jpg';break;
							case 'png': $format = 'png';break;
							case 'bmp': $format = 'bmp';break;
						}
						
						$timestamp = time();
						
						$file_name = md5($image['name'].$timestamp).'.'.$format;
						
						$oPicture = new Picture;
						
						if(!file_exists('pictures/'.$file_name))
						{
							$temp_loc = 'temp/';
							$temp_loc_small = 'temp_small/';
							
							$copy = move_uploaded_file($image['tmp_name'], $temp_loc.$file_name);	

							$copy_small = copy($temp_loc.$file_name, $temp_loc_small.$file_name);
							
							if($copy && $copy_small) 
							{	
								// first big picture options
								$theight = 480;
								$twidth = 640;
								$location = 'pictures/';
								
								// second small picture options
								$theight_small = 100;
								$twidth_small = 150;
								$location_small = 'small_pictures/';
								
								if($oPicture->convert_image($image, $theight, $twidth, $location, $temp_loc, $file_name) && $oPicture->convert_image($image, $theight_small, $twidth_small, $location_small, $temp_loc_small, $file_name))
								{
									$place = $location.$file_name;
								
									if(strlen($description) == 0)
									{
										$description = 'none';
									}
													
									$query = $connect->query("INSERT INTO pictures (image, description) VALUES('$place', '$description')");
														
									if($query)
									{
										echo '<b>The image was successfully uploaded.</b>';
										unlink($temp_loc.$file_name);
										unlink($temp_loc_small.$file_name);
									}
									else
									{
										echo 'Error with database!';
										unlink($temp_loc.$file_name);
										unlink($temp_loc_small.$file_name);
										unlink('pictures/'.$file_name);
										unlink('small_pictures/'.$file_name);
									}	
								}
								else
								{
									echo 'Error when uploading the picture!';
									unlink($temp_loc.$file_name);
									unlink($temp_loc_small.$file_name);
								}
							}
							else
							{
								echo 'Error when copying the picture!';
								unlink($temp_loc.$file_name);
								unlink($temp_loc_small.$file_name);
							}
						}
					}
					else
					{
						echo 'Check the image!';
					}
				}
				?>
				<form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=2" method="post" enctype="multipart/form-data">
					<b>Image : </b>
					<input type="file" name="image" id="image" />
					<b>Description : </b>
					<input type="text" name="description" id="description" />
					<input type="submit" value="Upload >" name="submit" />
				</form>
			</article>
		</section>
		<footer>
			<br />Designed and developed by Ivan Iliev.
		</footer>
	</body>
</html>